import { _decorator, Component, instantiate, Node, Prefab,math } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('EnermyManager')
export class EnermyManager extends Component {

    @property
    enermy0SpawnRate: number = 1;

    @property(Prefab)
    enermy0Prefab: Prefab = null;

    @property
    enermy1SpawnRate: number = 3;

    @property(Prefab)
    enermy1Prefab: Prefab = null;

    @property
    enermy2SpawnRate: number = 10;

    @property(Prefab)
    enermy2Prefab: Prefab = null;
    

    start() {
        this.schedule(this.enermy0Spawn, this.enermy0SpawnRate);
        this.schedule(this.enermy1Spawn, this.enermy1SpawnRate);
        this.schedule(this.enermy2Spawn, this.enermy2SpawnRate);
    }

    update(deltaTime: number) {
        
    }

    protected onDestroy(): void {
        this.unschedule(this.enermy0Spawn);
        this.unschedule(this.enermy1Spawn);
        this.unschedule(this.enermy2Spawn);
    }

    enermy0Spawn(){
        this.enermySpawn(this.enermy0Prefab, -215, 215, 450)
    }

    enermy1Spawn(){
        this.enermySpawn(this.enermy1Prefab, -215, 215, 450)
    }

    enermy2Spawn(){
        this.enermySpawn(this.enermy2Prefab, -215, 215, 450)
    }

    enermySpawn(enermyPrefab:Prefab,minX:number,maxX:number,Y:number) {
        
        const enermy = instantiate(enermyPrefab);
        this.node.addChild(enermy);

        const randomX = math.randomRange(minX, maxX);
        enermy.setPosition(randomX, Y, 0);
    }
}


